package org.example.dao;

import org.example.entity.Teacher;
import org.example.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TeacherDao {


    // List ArrayList
    public static int addUser(Teacher user) {

        // 泛型

        // 查询数据
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "insert into teacher (Id,Sname,Sno,Gender,Prof,Address,Grade) values (?,?,?,?,?,?,?)";
        int row = 0;
        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getId());
            statement.setString(2, user.getSname());
            statement.setString(3, user.getSno());
            statement.setString(4, user.getGender());
            statement.setString(5, user.getProf());
            statement.setString(6, user.getAddress());
            statement.setString(7, user.getGrade());


            // 执行SQL语句
            // select ----> statement.executeQuery();

            // insert delete update
            row = statement.executeUpdate();

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return row;
    }

    public static int deleteUser(String Userid) {
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "delete from teacher where Id=?";
        int row = 0;

        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);
            statement.setString(1, Userid);

            // 执行SQL语句
            // select ----> statement.executeQuery();

            // insert delete update
            row = statement.executeUpdate();

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return row;
    }

    public static int updateUser(Teacher user) {
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "update teacher set Sname=?,Sno = ?,Gender =  ?, Prof = ?, Address = ?, Grade = ? where Id = ?";
        int row = 0;
        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getSname());
            statement.setString(2, user.getSno());
            statement.setString(3, user.getGender());
            statement.setString(4, user.getProf());
            statement.setString(5, user.getAddress());
            statement.setString(6, user.getGrade());
            statement.setString(7, user.getId());


            // 执行SQL语句
            // select ----> statement.executeQuery();

            // insert delete update
            row = statement.executeUpdate();

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return row;
    }


}

